package com.city.itis.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.city.itis.dao.MemberCategoryDao;
import com.city.itis.domain.MemberCategory;


/**
 * 会员类别Dao实现类
 * @author WY
 *
 */
@Component("memberCateogryDao")
public class MemberCategoryDaoImpl implements MemberCategoryDao {
	
	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public int save(MemberCategory memberCategory) 
	{
		
		try
		{
			int id=(Integer)this.hibernateTemplate.save(memberCategory);
			return id;
			
		}catch(Exception e)
		{
			return 0;
		}
	}

	@Override
	public int delete(MemberCategory memberCategory) {
		try {
			this.hibernateTemplate.delete(memberCategory);
		} catch (Exception e) {
		return 0;
		}
		return 1;
	}

	@Override
	public int deleteById(Integer id) {
		MemberCategory memberCategory=(MemberCategory) this.getHibernateTemplate().get(MemberCategory.class, id);
		if(memberCategory!=null){
		try {
			this.hibernateTemplate.delete(memberCategory);
		} catch (Exception e) {
			return 0;
		}
		return 1;
		}
		return 0;
	}

	@Override
	public int update(MemberCategory memberCategory) {
		try {
			this.hibernateTemplate.update(memberCategory);
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}

	@Override
	public MemberCategory getMemberCategoryById(Integer id) {
		MemberCategory memberCategory=(MemberCategory)this.getHibernateTemplate().get(MemberCategory.class, id);
	     if(memberCategory!=null){
	    	 return memberCategory;
	     }
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public MemberCategory getMemberCategoryByMemberCategory(String memberCategory) {
		String queryString="FROM MemberCategory mc where mc.name=?";
		List<MemberCategory> memberCategoryList=(List<MemberCategory>)this.getHibernateTemplate().find(queryString,memberCategory);
		if(memberCategory!=null)
		{
			for(MemberCategory mc:memberCategoryList)
			{
				return mc;
			}
		}
		return null;
	}

	@Override
	public List<MemberCategory> findAll() {
		String queryString="FROM MemberCategory order by id desc ";
		@SuppressWarnings("unchecked")
		List<MemberCategory> memberCategoryList=(List<MemberCategory>)this.hibernateTemplate.find(queryString);
		if(memberCategoryList!=null){
			return memberCategoryList;
		}
		return null;
	}
	
	

}
